﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;



namespace ExaminationSys.View.Paper
{
    public partial class PaperFeedbackList : System.Web.UI.Page
    {

 
        private static readonly PaperFeedbackService Proxy = ObjectFactory<PaperFeedbackService>.Create();
        private static readonly PaperFeedBackCommentService PaperFeedBackCommentProxy = ObjectFactory<PaperFeedBackCommentService>.Create();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                PageEntity = null;
                 string PaperFeedBackCommentID = Request.QueryString["PaperFeedBackCommentID"] == null ? string.Empty : Request.QueryString["PaperFeedBackCommentID"].ToString();

                PaperFeedBackComment paperFeedBackCommentEntity = PaperFeedBackCommentProxy.Get(new Guid(PaperFeedBackCommentID));
                if(paperFeedBackCommentEntity!=null)
                {
                    textTitle.Text = paperFeedBackCommentEntity.Title;
                 
                }
    

                Session.Add("PaperFeedBackCommentID", PaperFeedBackCommentID);
            }
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            this.PaperFeedbackEditWindow.GridStore = this.StorePaperFeedback;
        }

        private int pageSize = 20;
        public static PageEntity PageEntity = null;
        protected void PaperFeedback_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            string sqlWhere = " 1=1";
            int pageIndex = 0;
            if (PageEntity != null)
            {
                sqlWhere = PageEntity.SqlWhere;
                pageIndex = 1;
            }
            else
            {
                string pcid =  Session["PaperFeedBackCommentID"].ToString();
                sqlWhere += string.Format(" and PaperFeedBackCommentID='{0}' ", pcid);
                sqlWhere += "and status<>0 ";
                pageIndex = (e.Start / e.Limit) + 1;

            }

            int totalPage, totalRecord;
            this.StorePaperFeedback.DataSource = Proxy.GetListByPage(sqlWhere, " OperatorTime desc", pageSize, pageIndex,
                                                                   out totalPage, out totalRecord);
            e.Total = totalRecord;

            this.StorePaperFeedback.DataBind();
        }

        public void SearchFun(PageEntity pageEntity)
        {
            PageEntity = pageEntity;
        }

        public void Btn_GoBack(object sender, DirectEventArgs e)
        {
            Response.Redirect("PaperFeedBackCommentList.aspx");
        }

        protected void Command(object sender, DirectEventArgs e)
        {
            var command = e.ExtraParams["command"];

            var id = e.ExtraParams["Id"];
            if (string.IsNullOrWhiteSpace(id))
            {
                MessageBoxHelper.ShowInfo("获取ID失败");
                return;
            }

            if (command.Equals("edit", StringComparison.OrdinalIgnoreCase))
            {
                ShowDetails(id);
            }
            else
            {
                var entity = Proxy.Get(new Guid(id));
                if (entity != null)
                {
                    entity.Status = 0;
                    var success = Proxy.Update(entity);
                    if (success)
                    {
                        StorePaperFeedback.Remove(entity.ID);
                        MessageBoxHelper.ShowInfo("删除成功");
                    }
                }
            }
        }

        protected void Add_Click(object sender, DirectEventArgs e)
        {
            if(textTitle.Text.Trim()==string.Empty)
            {
                 MessageBoxHelper.ShowInfo("请先输入评语标题!");
                    
                return;
            }


            PaperFeedBackComment paperFeedBackCommentEntity = PaperFeedBackCommentProxy.Get(new Guid(Session["PaperFeedBackCommentID"].ToString()));
            
            //新建评语
            if(paperFeedBackCommentEntity==null)
            {
                paperFeedBackCommentEntity = new PaperFeedBackComment();
                paperFeedBackCommentEntity.ID = new Guid(Session["PaperFeedBackCommentID"].ToString());  //新的GUID在前面一页面已经生成
                paperFeedBackCommentEntity.Title = textTitle.Text;
                paperFeedBackCommentEntity.SerialNo = DateTime.Now.ToString("yyyyMMddhhss");
                paperFeedBackCommentEntity.Status = 1;

                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    Response.Redirect("~/Account/Login.aspx");
                }
                paperFeedBackCommentEntity.Operator = op;

                paperFeedBackCommentEntity.OperatorTime = DateTime.Now;

                try
                {
                    PaperFeedBackCommentProxy.Insert(paperFeedBackCommentEntity);
       
                }
                catch (Exception ex)
                {
                    MessageBoxHelper.ShowInfo("保存失败!" + ex.ToString());
                    return;
                }
            }
            else
            {
                paperFeedBackCommentEntity.Title = textTitle.Text;
                paperFeedBackCommentEntity.SerialNo = DateTime.Now.ToString("yyyyMMddhhss");
                paperFeedBackCommentEntity.Status = 1;


                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    Response.Redirect("~/Account/Login.aspx");
                }
                paperFeedBackCommentEntity.Operator = op;

                paperFeedBackCommentEntity.OperatorTime = DateTime.Now;
                try
                {
                    PaperFeedBackCommentProxy.Update(paperFeedBackCommentEntity);
                }
                catch (Exception ex)
                {

                    MessageBoxHelper.ShowInfo("保存更新失败!" + ex.ToString());
                    return;
                }
            
            }

            this.HidPaperFeedBackCommentId.Value = paperFeedBackCommentEntity.ID.ToString();
            Session["PaperFeedBackCommentID"] = paperFeedBackCommentEntity.ID.ToString();

            this.PaperFeedbackEditWindow.SetData(string.Empty, Session["PaperFeedBackCommentID"].ToString());
            this.PaperFeedbackEditWindow.Show();
        }

        protected void BtnGroupFind_Click(object sender, DirectEventArgs e)
        {
            var text = GroupSearchText.Text.Trim();
            if (!string.IsNullOrWhiteSpace(text))
            {
                text = text.SqlSafe();
                var sqlWhere =string.Empty;

                sqlWhere += string.Format(" Comment like '%{0}%' and PaperFeedBackCommentID='{1}' ",text, new Guid(Session["PaperFeedBackCommentID"].ToString()));
                
                sqlWhere += "and status<>0 ";

                PageEntity = new PageEntity();
                PageEntity.SqlWhere = sqlWhere;
            }
            else
            {
                PageEntity = null;
            }
            StorePaperFeedback.Reload();
        }


        [DirectMethod]
        public void DoConfirm()
        {
            RowSelectionModel sm = GridPanelPaperFeedback.SelectionModel.Primary as RowSelectionModel;

            if (sm.SelectedRows.Count == 0)
            {
                MessageBoxHelper.ShowWarning("请选择要删除的记录");
                return;
            }
            X.Msg.Confirm("提示", "是否删除选中记录?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "X.DoDelete()",
                    Text = "是"
                },
                No = new MessageBoxButtonConfig
                {
                    Text = "否"
                }
            }).Show();
        }
        [DirectMethod]
        public void DoDelete()
        {
            RowSelectionModel sm = GridPanelPaperFeedback.SelectionModel.Primary as RowSelectionModel;

            if (sm.SelectedRows.Count == 0)
            {
                MessageBoxHelper.ShowWarning("请选择要删除的记录");
                return;
            }

            List<string> ids = new List<string>();
            foreach (var row in sm.SelectedRows)
            {
                ids.Add(row.RecordID);
            }
            var success = Proxy.Delete(ids);
            if (success)
            {
                ids.ForEach(id => StorePaperFeedback.Remove(id));
                MessageBoxHelper.ShowInfo("保存成功");
            }
        }
        protected void BatchDelete_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = GridPanelPaperFeedback.SelectionModel.Primary as RowSelectionModel;

            if (sm.SelectedRows.Count == 0)
            {
                MessageBoxHelper.ShowWarning("请选择要删除的记录");
                return;
            }
            List<string> ids = new List<string>();
            foreach (var row in sm.SelectedRows)
            {
                ids.Add(row.RecordID);
            }
            var success = Proxy.Delete(ids);
            if (success)
            {
                ids.ForEach(id => StorePaperFeedback.Remove(id));
                MessageBoxHelper.ShowInfo("删除成功");
            }
        }
        protected void BatchLock_Click(object sender, DirectEventArgs e)
        {
            //RowSelectionModel sm = GridPanelPaperFeedbackType.SelectionModel.Primary as RowSelectionModel;

            //if (sm.SelectedRows.Count == 0)
            //{
            //    MessageBoxHelper.ShowWarning("请选择要锁定的记录");
            //    return;
            //}
            //List<string> ids = new List<string>();
            //foreach (var row in sm.SelectedRows)
            //{
            //    ids.Add(row.RecordID);
            //}
            var success = true;// Proxy.Lock(ids, 2);
            if (success)
            {
                StorePaperFeedback.Reload();
                MessageBoxHelper.ShowInfo("保存成功");
            }
        }

        protected void BatchUnLock_Click(object sender, DirectEventArgs e)
        {
            //RowSelectionModel sm = GridPanelPaperFeedbackType.SelectionModel.Primary as RowSelectionModel;

            //if (sm.SelectedRows.Count == 0)
            //{
            //    MessageBoxHelper.ShowWarning("请选择要解锁的记录");
            //    return;
            //}
            //List<string> ids = new List<string>();
            //foreach (var row in sm.SelectedRows)
            //{
            //    ids.Add(row.RecordID);
            //}
            //var success = Proxy.Lock(ids, 1);
            //if (success)
            //{
            //    StorePaperFeedback.Reload();
            //    MessageBoxHelper.ShowInfo("保存成功");
            //}
        }

        protected void BtnSearch_Click(object sender, DirectEventArgs e)
        {
            //  this.PaperFeedbackSearchWindow.Show(SearchFun);
        }

        protected void ShowDetails(string id)
        {
            var realId = new Guid(id);
            var result = Proxy.Get(realId);
            if (result != null)
            {
                PaperFeedbackEditWindow.GridStore = StorePaperFeedback;
                this.PaperFeedbackEditWindow.SetData(id, Session["PaperFeedBackCommentID"].ToString());
                this.PaperFeedbackEditWindow.Show();
            }
        }
    }
}